package com.yf1706.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yf1706.bean.User;
import com.yf1706.service.UserService;
import com.yf1706.tools.UserTools;

@Controller
@RequestMapping("log")
public class LoginController {
	@Autowired
	private UserService us;
	
	@RequestMapping("toLogin")
	public String toLogin() {
		return "zxy/login";
	}

	@RequestMapping("identify")
	public void identify(String userId,String psw,HttpServletRequest request,HttpServletResponse response) {
		System.out.println("进入登录验证...."+"userId:"+userId+",psw:"+psw);

		try {
			PrintWriter out=response.getWriter();
			if(UserTools.userIdUnformat(userId)==-1) {
				out.print("2");
			}
			User user=us.login(UserTools.userIdUnformat(userId), psw);
			System.out.println(user);
			if(user!=null) {
				HttpSession session=request.getSession();
				session.setAttribute("user", user);
				out.print("1");
			}
			else {
				out.print("3");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
